﻿using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuService.CommandServices;
using QuanLyGiaoVuService.CommonServices;
using QuanLyGiaoVuService.ReportServices;
using QuanLyGiaoVuService.Utilities;
using QuanLyGiaoVuUI.Extensions.ExtendedAttributes;
using QuanLyGiaoVuUI.Models.GiangVien;
using QuanLyGiaoVuUI.Models.GiangVienClient;
using QuanLyGiaoVuUI.Models.TienDoDeTai;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QuanLyGiaoVuUI.Controllers
{
    [GiangVienAuthorize(ChiGiaoVu = true)]
    public class TienDoDeTaiController : Controller
    {
        private IGiangVienReportService _giangVienReportService;
        private IGiangVienCommandService _giangVienCommandService;
        private ITienDoDeTaiCommandService _tienDoDeTaiCommandService;
        private ITienDoDeTaiReportService _tienDoDeTaiReportService;
        private IThamSoService _thamSoService;
        private IHocKyReportService _hocKyReportService;
        private IDeTaiCommandService _deTaiCommandService;
        private IDeTaiReportService _deTaiReportService;

        private ITaiLieuReportService _taiLieuReportService;
        private ITaiLieuCommandService _taiLieuCommandService;
        private IFileService _fileService;
        private IQuanLyQuyenTruyCapService _quyenTruyCapService;


        private IDangKyDeTaiCommandService _dangKyDeTaiCommandService;
        private IDangKyDeTaiReportService _dangKyDeTaiReportService;
        private ISinhVienCommandService _sinhVienCommandService;
        private ISinhVienReportService _sinhVienReportService;

        public TienDoDeTaiController(IGiangVienCommandService giangVienCommandService,
            IGiangVienReportService giangVienReportService,
            ITienDoDeTaiCommandService tienDoDeTaiCommandService,
            ITienDoDeTaiReportService tienDoDeTaiReportService,
            IThamSoService thamSoService,
            IHocKyReportService hocKyReportService,
            IDeTaiCommandService deTaiCommandService,
            IDeTaiReportService deTaiReportService,
            ITaiLieuReportService taiLieuReportService,
            ITaiLieuCommandService taiLieuCommandService,
            IFileService fileService,
            IQuanLyQuyenTruyCapService quyenTruyCapService,
            IDangKyDeTaiCommandService dangKyDeTaiCommandService,
            IDangKyDeTaiReportService dangKyDeTaiReportService,
            ISinhVienCommandService sinhVienCommandService,
            ISinhVienReportService sinhVienReportService)
        {
            _giangVienReportService = giangVienReportService;
            _giangVienCommandService = giangVienCommandService;
            _tienDoDeTaiCommandService = tienDoDeTaiCommandService;
            _tienDoDeTaiReportService = tienDoDeTaiReportService;
            _thamSoService = thamSoService;
            _hocKyReportService = hocKyReportService;
            _deTaiCommandService = deTaiCommandService;
            _deTaiReportService = deTaiReportService;
            _taiLieuCommandService = taiLieuCommandService;
            _taiLieuReportService = taiLieuReportService;
            _fileService = fileService;
            _quyenTruyCapService = quyenTruyCapService;
            _dangKyDeTaiCommandService = dangKyDeTaiCommandService;
            _dangKyDeTaiReportService = dangKyDeTaiReportService;
            _sinhVienCommandService = sinhVienCommandService;
            _sinhVienReportService = sinhVienReportService;
        }
        //
        // GET: /TienDoDeTai/

        public ActionResult Index()
        {
            QuanLyDeTaiViewModel deTaiViewModel = new QuanLyDeTaiViewModel();
            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            HocKyDTO hocKyDTO = new HocKyDTO();
            Guid MaNamHoc = _hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;
            IEnumerable<DeTaiDTO> deTaiGiangVien = _deTaiReportService.GetAllDeTaiNamHoc(MaNamHoc); //get theo mahocky 

            List<DeTaiDTO> tempDeTai = new List<DeTaiDTO>();

            foreach (var detaiGV in deTaiGiangVien)
            {
                IEnumerable<TienDoDeTaiDTO> tiendoDTO = _tienDoDeTaiReportService.GetAllTienDoDeTai(detaiGV.MaDeTai);
                TienDoDeTaiDTO tiendo = new TienDoDeTaiDTO();
                if (tiendoDTO != null && tiendoDTO.Count() > 0)
                {                                        
                    tiendo.TienDoHoanThanh = 0;
                    foreach (var td in tiendoDTO)
                    {
                        if (td.TienDoHoanThanh >= tiendo.TienDoHoanThanh)
                        {
                            tiendo = td;
                        }
                    }                                        
                }
                detaiGV.GhiChu = "(" + tiendoDTO.Count().ToString() + ")(" + tiendo.TienDoHoanThanh.ToString() + "%)";
                tempDeTai.Add(detaiGV);
            }
            deTaiViewModel.danhSachDeTai = deTaiGiangVien;
            return View(deTaiViewModel);
        }
        [HttpGet]
        public ActionResult TienDoDeTai(Guid Id)
        {            
            TienDoDeTaiViewModel tienDoDeTaiViewModel = new TienDoDeTaiViewModel();
            tienDoDeTaiViewModel.DanhSachTienDoDT = _tienDoDeTaiReportService.GetAllTienDoDeTai(Id);
            return View(tienDoDeTaiViewModel);
        }
        [HttpGet]
        public ActionResult DeTai()
        {            
            DeTaiGiangVienClientViewModel deTaiGVViewModel = new DeTaiGiangVienClientViewModel();
            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            HocKyDTO hocKyDTO = new HocKyDTO();
            Guid MaNamHoc = _hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;
            deTaiGVViewModel.namHoc = _hocKyReportService.GetNamHocById(MaNamHoc);
            IEnumerable<DeTaiDTO> deTaiGiangVien = _deTaiReportService.GetAllDeTai(null, null, hocKyHienTai, MaNamHoc, null, null);
            if (deTaiGiangVien == null) deTaiGiangVien = new List<DeTaiDTO>();
            
            deTaiGVViewModel.deTaiGiangVien = deTaiGiangVien;
            return View(deTaiGVViewModel);
        }
        [HttpGet]
        public ActionResult ThongKeDeTai(int tile)
        {
            DeTaiGiangVienClientViewModel deTaiGVViewModel = new DeTaiGiangVienClientViewModel();
            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            HocKyDTO hocKyDTO = new HocKyDTO();
            Guid MaNamHoc = _hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;
            deTaiGVViewModel.namHoc = _hocKyReportService.GetNamHocById(MaNamHoc);
            IEnumerable<DeTaiDTO> deTaiGiangVien = _deTaiReportService.GetAllDeTai(null, null, hocKyHienTai, null, null, null);
            if (deTaiGiangVien == null) deTaiGiangVien = new List<DeTaiDTO>();                        

            List<DeTaiDTO> tempDeTai = new List<DeTaiDTO>();

            foreach (var detaiGV in deTaiGiangVien)
            {
                IEnumerable<TienDoDeTaiDTO> tiendoDTO = _tienDoDeTaiReportService.GetAllTienDoDeTai(detaiGV.MaDeTai);
                if (tile > 0)
                {
                    if (tiendoDTO != null && tiendoDTO.Count() > 0)
                    {

                        //TienDoDeTaiDTO tiendo = tiendoDTO.ToList().Where(p => (p.TienDoHoanThanh > tile)).OrderBy(p => p.TienDoHoanThanh).First();                                                
                        TienDoDeTaiDTO tiendo = new TienDoDeTaiDTO();
                        bool hasTienDo = false;
                        foreach (var td in tiendoDTO)
                        {
                            if (td.TienDoHoanThanh >= tile)
                            {
                                tiendo = td;
                                hasTienDo = true;
                            }
                        }
                        if (hasTienDo)
                        {
                            detaiGV.GhiChu = tiendo.TienDoHoanThanh.ToString();
                            tempDeTai.Add(detaiGV);
                        }
                    }
                }
                else
                {
                    TienDoDeTaiDTO tiendo = new TienDoDeTaiDTO();
                    tiendo.TienDoHoanThanh = 0;
                    if (tiendoDTO != null)
                    {
                        foreach (var td in tiendoDTO)
                        {
                            if (td.TienDoHoanThanh >= tiendo.TienDoHoanThanh)
                            {
                                tiendo = td;
                            }
                        }
                        detaiGV.GhiChu = tiendo.TienDoHoanThanh.ToString();
                        
                    }
                    else
                    {
                        detaiGV.GhiChu = "0";
                    }
                    if (tile == ThamSoConstantUtility.TATCATIENDO)
                    {
                        tempDeTai.Add(detaiGV);
                    }
                    else
                    {
                        if (tile == ThamSoConstantUtility.DUOCBAOCAOTOTNGHIEP)
                        {
                            if (detaiGV.KhoaLuanTotNghiep > 0)
                            {
                                tempDeTai.Add(detaiGV);
                            }
                        }
                    }
                }
            }
                     
            deTaiGVViewModel.deTaiGiangVien = tempDeTai;
            deTaiGVViewModel.deTaiGiangVien = deTaiGVViewModel.deTaiGiangVien.OrderByDescending(p => p.GhiChu);
            return View(deTaiGVViewModel);
        }
        [HttpGet]
        public ActionResult ThemDeTai()
        {
            DeTaiViewModel deTaiViewModel = new DeTaiViewModel();            
            deTaiViewModel.danhSachGiangVien = _giangVienReportService.GetAllGiangVien();
            Guid khoLuanVanID = Guid.Parse(_thamSoService.GetThamSo("MaThuMucGocKhoLuanVan"));
            deTaiViewModel.danhSachTaiLieuLuanVan = _taiLieuReportService.GetAllTaiLieu(khoLuanVanID, true, null, null, true);
            return View(deTaiViewModel);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThemDeTai(
            String tenDeTai,
            Guid gvHD1,
            Guid gvHD2,
            bool isKhoaLuanTN,
            String moTa,
            bool isDatYeuCau,
            String ghiChu,
            String fileTaiLieu,
            String sv1,
            String sv2)
        {
            bool isSuccess = true;
            String mess = "Thao tác thành công!";
            Guid hkHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            HocKyDTO hocky = _hocKyReportService.GetHocKyById(hkHienTai);

            if (tenDeTai.Trim() == "")
            {
                isSuccess = false;
                mess = "Tên đề tài không được rỗng!";
                return Json(new { success = isSuccess, message = mess });
            }
            SinhVienDTO sinhVien1 = new SinhVienDTO();
            SinhVienDTO sinhVien2 = new SinhVienDTO();

            //check sv1
            if (sv1.Trim() != "")
            {
                String masinhvien1=sv1.Trim();              
                sinhVien1 = _sinhVienReportService.GetSinhVienByMSSV(masinhvien1);
                if (sinhVien1 == null)
                {
                    isSuccess = false;
                    mess = "Mã sinh viên " + masinhvien1 + "không đúng!";
                    return Json(new { success = isSuccess, message = mess });
                }
                if ((!sinhVien1.DuDKLuanVan))
                {
                    isSuccess = false;
                    mess = "Sinh viên "+masinhvien1+" không đủ điều kiện đăng ký đề tài!";
                    return Json(new { success = isSuccess, message = mess });
                }


                IEnumerable<DangKyDeTaiDTO> dsDeTaiDaDK = _dangKyDeTaiReportService.GetDangKyDeTaiBySinhVienId(sinhVien1.ID, hocky.MaNamHoc);
                if (dsDeTaiDaDK != null)
                {
                    if (dsDeTaiDaDK.Count() > 0)
                    {
                        isSuccess = false;
                        mess = "Sinh viên" + sinhVien1.MaSinhVien + " đã tồn tại đăng ký đề tài!!!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }

                IEnumerable<DangKyDeTaiDTO> dsDeTaiDaSinhVienKhacDK = _dangKyDeTaiReportService.GetDangKyDeTaiByMSSV(sinhVien1.MaSinhVien, hocky.MaNamHoc);
                if (dsDeTaiDaSinhVienKhacDK != null)
                {
                    if (dsDeTaiDaSinhVienKhacDK.Count() > 0)
                    {
                        isSuccess = false;
                        mess = mess = "Sinh viên" + sinhVien1.MaSinhVien + " đã tồn tại đăng ký đề tài!!!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }

            }
            if (sv2.Trim() != "")
            {
                String masinhvien1 = sv2.Trim();
                sinhVien2 = _sinhVienReportService.GetSinhVienByMSSV(masinhvien1);
                if (sinhVien2 == null)
                {
                    isSuccess = false;
                    mess = "Mã sinh viên " + masinhvien1 + "không đúng!";
                    return Json(new { success = isSuccess, message = mess });
                }
                if ((!sinhVien2.DuDKLuanVan))
                {
                    isSuccess = false;
                    mess = "Sinh viên " + masinhvien1 + " không đủ điều kiện đăng ký đề tài!";
                    return Json(new { success = isSuccess, message = mess });
                }
                IEnumerable<DangKyDeTaiDTO> dsDeTaiDaDK2 = _dangKyDeTaiReportService.GetDangKyDeTaiBySinhVienId(sinhVien2.ID, hocky.MaNamHoc);
                if (dsDeTaiDaDK2 != null)
                {
                    if (dsDeTaiDaDK2.Count() > 0)
                    {
                        isSuccess = false;
                        mess = "Sinh viên" + sinhVien2.MaSinhVien + " đã tồn tại đăng ký đề tài!!!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }

                IEnumerable<DangKyDeTaiDTO> dsDeTaiDaSinhVienKhacDK2 = _dangKyDeTaiReportService.GetDangKyDeTaiByMSSV(sinhVien2.MaSinhVien, hocky.MaNamHoc);
                if (dsDeTaiDaSinhVienKhacDK2 != null)
                {
                    if (dsDeTaiDaSinhVienKhacDK2.Count() > 0)
                    {
                        isSuccess = false;
                        mess = mess = "Sinh viên" + sinhVien2.MaSinhVien + " đã tồn tại đăng ký đề tài!!!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }
            }



            DeTaiDTO deTaiDTO = new DeTaiDTO();
            deTaiDTO.MaDeTai = Guid.NewGuid();
            deTaiDTO.TenDeTai = tenDeTai;
            deTaiDTO.GiangVienHD1 = gvHD1;
            deTaiDTO.GiangVienHD2 = gvHD2;
            deTaiDTO.KhoaLuanTotNghiep = (isKhoaLuanTN == true) ? 1 : 0;
            deTaiDTO.MoTa = moTa;
            deTaiDTO.DatYeuCau = isDatYeuCau;
            deTaiDTO.GhiChu = ghiChu;
            deTaiDTO.FileTaiLieu = fileTaiLieu;
            deTaiDTO.MaHocKy = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai"));
            //if (_deTaiCommandService.InsertDeTai(deTaiDTO) == null)
            //{
            //    isSuccess = false;
            //    mess = "Thao tác không thành công!";
            //    return Json(new { success = isSuccess, message = mess });
            //}
            sv1 = (sv1.Trim().Equals("") ? sv2 : sv1);
            sv2 = (sv2.Trim().Equals("") ? sv1 : sv2);
            if(sv1.Trim().Equals("") && sv2.Trim().Equals(""))
            {
                if (_deTaiCommandService.InsertDeTai(deTaiDTO) == null)
                {
                    isSuccess = false;
                    mess = "Thao tác không thành công!";
                    return Json(new { success = isSuccess, message = mess });
                }
                else
                {
                    isSuccess = true;
                    mess = "Thao tác thành công!";
                    return Json(new { success = isSuccess, message = mess });
                }
            }
            else
            {
                if (_sinhVienReportService.GetSinhVienByMSSV(sv1) == null || _sinhVienReportService.GetSinhVienByMSSV(sv2) == null)
                {
                    isSuccess = false;
                    mess = "Mã sinh viên không tồn tại!";
                    return Json(new { success = isSuccess, message = mess });
                }
                else
                {
                    //IEnumerable<DangKyDeTaiDTO> dk1 = _dangKyDeTaiReportService.GetDangKyDeTaiByMSSV(sv1, Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai")));
                    IEnumerable<DangKyDeTaiDTO> dk1 = _dangKyDeTaiReportService.GetDangKyDeTaiByMSSV(sv1, hocky.MaNamHoc);

                    if (dk1!= null && dk1.Count()>0)
                    {
                        isSuccess = false;
                        mess = "Sinh viên " + sv1 +" đã tồn tại đk đề tài!";
                        return Json(new { success = isSuccess, message = mess });
                    }

                    //IEnumerable<DangKyDeTaiDTO> dk2 = _dangKyDeTaiReportService.GetDangKyDeTaiByMSSV(sv2, Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai")));
                    IEnumerable<DangKyDeTaiDTO> dk2 = _dangKyDeTaiReportService.GetDangKyDeTaiByMSSV(sv2, hocky.MaNamHoc);

                    if (dk2 != null && dk2.Count() > 0)
                    {
                        isSuccess = false;
                        mess = "Sinh viên " + sv2 + " đã tồn tại đk đề tài!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }

                if (_deTaiCommandService.InsertDeTai(deTaiDTO) != null)
                {
                    DangKyDeTaiDTO dkdtDTO = new DangKyDeTaiDTO();
                    dkdtDTO.MaDangKyDeTai = Guid.NewGuid();
                    dkdtDTO.MaDeTai = deTaiDTO.MaDeTai;
                    dkdtDTO.MaNamHoc = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai"));
                    dkdtDTO.NgayDangKy = DateTime.Now;
                    dkdtDTO.SinhVienDangKy = Guid.Empty;
                    dkdtDTO.MaSinhVien1 = sv1;
                    dkdtDTO.MaSinhVien2 = sv2;
                    if (_dangKyDeTaiCommandService.InsertDangKyDeTai(dkdtDTO) == null)
                    {
                        _deTaiCommandService.DeleteDeTai(deTaiDTO.MaDeTai);
                        isSuccess = false;
                        mess = "Thao tác không thành công!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }
                else
                {
                    isSuccess = true;
                    mess = "Thao tác thành công!";
                    return Json(new { success = isSuccess, message = mess });
                }
            }
            return Json(new { success = isSuccess, message = mess });
        }
        [HttpGet]
        public ActionResult SuaDeTai(Guid id)
        {
            DeTaiViewModel deTaiViewModel = new DeTaiViewModel();
            deTaiViewModel.DeTai = _deTaiReportService.GetDeTaiById(id);
            deTaiViewModel.danhSachGiangVien = _giangVienReportService.GetAllGiangVien();
            Guid khoLuanVanID=Guid.Parse(_thamSoService.GetThamSo("MaThuMucGocKhoLuanVan"));
            //Guid test = Guid.Parse("6916cad9-138a-48bd-9d94-ad164a8b3d8b"); // just 4 test
            deTaiViewModel.danhSachTaiLieuLuanVan = _taiLieuReportService.GetAllTaiLieu(khoLuanVanID, true, null, null, true);
            if (!deTaiViewModel.DeTai.FileTaiLieu.Equals(""))
            {
                TaiLieuDTO tailieu = _taiLieuReportService.GetTaiLieuById(Guid.Parse(deTaiViewModel.DeTai.FileTaiLieu));
                deTaiViewModel.tenFileTaiLieu = tailieu.TenTaiLieu;
            }
            else
            {
                deTaiViewModel.tenFileTaiLieu = "";
            }
            //get sv infor
            DangKyDeTaiDTO dkDT = _dangKyDeTaiReportService.GetDangKyDeTaiByDeTaiId(id);
            if (dkDT != null)
            {
                deTaiViewModel.sinhVien1 = _sinhVienReportService.GetSinhVienByMSSV(dkDT.MaSinhVien1);
                deTaiViewModel.sinhVien2 = _sinhVienReportService.GetSinhVienByMSSV(dkDT.MaSinhVien2);
            }
            return View(deTaiViewModel);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SuaDeTai(Guid maDeTai,
            String tenDeTai,
            Guid gvHD1,
            Guid gvHD2,
            bool isKhoaLuanTN,
            String moTa,
            //bool isDaDuyet,
            bool isDatYeuCau,
            String ghiChu,
            String fileTaiLieu,
            String sv1,
            String sv2)
        {
            bool isSuccess = true;
            String mess = "Thao tác thành công!";
            if (tenDeTai.Trim() == "")
            {
                isSuccess = false;
                mess = "Tên đề tài không được rỗng!";
                return Json(new { success = isSuccess, message = mess });
            }

                       
            DeTaiDTO deTaiDTO = _deTaiReportService.GetDeTaiById(maDeTai);
            deTaiDTO.MaDeTai = maDeTai;
            deTaiDTO.TenDeTai = tenDeTai;
            deTaiDTO.GiangVienHD1 = gvHD1;
            deTaiDTO.GiangVienHD2 = gvHD2;
            deTaiDTO.KhoaLuanTotNghiep = (isKhoaLuanTN == true) ? 1 : 0;
            deTaiDTO.MoTa = moTa;            
            deTaiDTO.DatYeuCau = isDatYeuCau;
            deTaiDTO.GhiChu = ghiChu;            
            //deTaiDTO.FileTaiLieu = (fileTaiLieu==null?"":(fileTaiLieu==""?"":deta));
            if (fileTaiLieu != null && fileTaiLieu != "")
            {
                deTaiDTO.FileTaiLieu = fileTaiLieu;
            }

            if (_deTaiCommandService.UpdateDeTai(deTaiDTO) == null)
            {
                isSuccess = false;
                mess = "Thao tác không thành công!";
                return Json(new { success = isSuccess, message = mess });
            }
            else
            {
                DangKyDeTaiDTO dkdtDTO = _dangKyDeTaiReportService.GetDangKyDeTaiByDeTaiId(maDeTai);
                if (dkdtDTO == null && !sv1.Equals("") && !sv2.Equals(""))
                {
                    dkdtDTO = new DangKyDeTaiDTO();
                    dkdtDTO.MaDangKyDeTai = Guid.NewGuid();
                    dkdtDTO.MaDeTai = maDeTai;
                    dkdtDTO.MaNamHoc = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai"));
                    dkdtDTO.SinhVienDangKy = Guid.NewGuid();
                    dkdtDTO.NgayDangKy = DateTime.Now;
                    dkdtDTO.MaSinhVien1 = sv1;
                    dkdtDTO.MaSinhVien2 = sv2;

                    if (_dangKyDeTaiCommandService.InsertDangKyDeTai(dkdtDTO) == null)
                    {
                        isSuccess = false;
                        mess = "Cập nhật sinh viên làm đề tài không thành công!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }
                else
                {
                    if (dkdtDTO != null)
                    {
                        dkdtDTO.MaSinhVien1 = sv1;
                        dkdtDTO.MaSinhVien2 = sv2;
                        if (_dangKyDeTaiCommandService.UpdateDangKyDeTai(dkdtDTO) == null)
                        {
                            isSuccess = false;
                            mess = "Cập nhật sinh viên làm đề tài không thành công!";
                            return Json(new { success = isSuccess, message = mess });
                        }
                    }
                }
            }
            return Json(new { success = isSuccess, message = mess });
        }
        [HttpPost]
        public ActionResult CheckNameSinhVien(String sv1, String sv2)
        {
            String nameSV1 = "sinh viên không tồn tại";
            String nameSV2 = "sinh viên không tồn tại";

            if (sv1 != null)
            {
               SinhVienDTO sv1DTO = _sinhVienReportService.GetSinhVienByMSSV(sv1);
               if (sv1DTO != null)
               {
                   nameSV1 = sv1DTO.HoTenSV;
               }
            }
            if (sv2 != null)
            {
                SinhVienDTO sv2DTO = _sinhVienReportService.GetSinhVienByMSSV(sv2);
                if (sv2DTO != null)
                {
                    nameSV2 = sv2DTO.HoTenSV;
                }
            }
            return Json(new { sv1 = nameSV1, sv2 = nameSV2 });
        }
        [HttpPost]
        public ActionResult XoaDeTai(String MaDeTai)
        {
            bool isSuccess = true;
            String mess = "Xóa thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro != 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            Guid madetai;
            try
            {
                madetai = Guid.Parse(MaDeTai);
            }
            catch (Exception ex)
            {
                isSuccess = false;
                mess = "Có lỗi xảy ra! Vui lòng thao tác lại lần nữa!";
                return Json(new { success = isSuccess, message = mess });
            }


            if (!_deTaiCommandService.DeleteDeTai(madetai))
            {
                isSuccess = false;
                mess = "Xóa không thành công!";
            }
            return Json(new { success = isSuccess, message = mess });
        }

    }
}
